package com.ml.servlet;

import com.ml.dao.DkDao;
import com.ml.dao.UserDao;
import com.ml.dao.impl.DkDaoImpl;
import com.ml.dao.impl.UserDaoImpl;
import com.ml.model.Dk;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author zzy
 * 废弃代码，留作备用
 */
public class WorkHour2 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        //PrintWriter out = res.getWriter();
        UserDao ud = new UserDaoImpl();
        DkDao dk = new DkDaoImpl();

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            String flag = request.getParameter("flag");

            //按照人查询
            String user = request.getParameter("user");
            String psd = request.getParameter("psd");
            Object sessionUser = request.getSession().getAttribute("user");

            if ((user == null || user.equals("")) && sessionUser == null) {
                request.setAttribute("msg", "用户名不能为空");
                request.getRequestDispatcher("/index.jsp").forward(request, response);
                return;
            } else {
                //放个sessiion
                if (sessionUser == null) {//第一次进入
                    if (!ud.isExistsUser(user)) {
                        //用户不存在
                        request.setAttribute("username", user);
                        request.setAttribute("msg", "用户不存在");
                        request.getRequestDispatcher("/index.jsp").forward(request, response);
                        return;
                    }else{//用户对了在给 session
                        request.getSession().setAttribute("user", user);
                        flag = psd;
                    }

                } else {//第2次进入
                    user = sessionUser.toString();
                }

                String searchUser = request.getParameter("searchuser");//要查询的人的名字
                if(flag!=null && flag.equals("admin")){//有搜索权限的
                    if(sessionUser == null){//说明是第一次加入flag
                        request.getSession().setAttribute("flag", flag);
                    }
                }else{//没有搜索权限的，只能查看自己的
                    searchUser = user;
                }


                List<Dk> list = new ArrayList<>();
                String month = request.getParameter("month");
                if (month == null) {//没有传递参数默认查询当月的
                    int m = new Date().getMonth() + 1;
                    month = m+""; // m > 9 ? (m + "") : ("0" + m);
                }


                //开始查询
                //按照名字查询


                request.setAttribute("month", month);//设置默认查询为当月
                request.setAttribute("list", list);//最终的查询结果
                request.getRequestDispatcher("/wk.jsp").forward(request, response);
                return;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //out.flush();
        //out.close();
        //response.sendRedirect("/ServletProject/fristServlet");//重定向
    }
}
